package com.jcl.core.db.page;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.db.page.model.PageCondition;
import com.jcl.core.db.page.model.PageTotal;
import com.jcl.core.utils.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DistributePage
 * @Author yyliu
 * @Date 2018/12/25 10:04
 * @Version 1.0
 **/
public class DistributePage<T> {

    public PageResult<T> pageResult(PageCondition condition) {
        PageTotal pageTotal = distributePageTotal.pageTotal(condition);
        Map<String, Integer[]> serverQueryCount = everyServerCurPageCount(pageTotal,
                condition.getCurPage(), condition.getRowSize());
        PageResult<T> pageResult = distributePageTotal.pageResult(condition, serverQueryCount);
        if(serverQueryCount.size() > 1){
            List<T> data = distributePageTotal.comparator(pageResult.getData());
            pageResult.setData(data);
        }
        pageResult.setTotal(pageTotal.getTotal());
        pageResult.setRowSize(condition.getRowSize());
        pageResult.setCurPage(condition.getCurPage());
        return pageResult;
    }

    public static Map<String, Integer[]> everyServerCurPageCount(PageTotal pageTotal,
                                                                 Integer curPage,
                                                                 Integer rowSize) {
        Map<String, Integer[]> everyServerCurPageCount = new HashMap<>();
        if (CollectionUtils.isNotEmpty(pageTotal.getServerList())) {
            int tempCount = curPage * rowSize;
            int decCount = rowSize;
            for (String serverName : pageTotal.getServerList()) {
                int curCount = pageTotal.getServerCount().get(serverName);
                tempCount -= curCount;
                if (tempCount < rowSize) {
                    if (tempCount < 0) {
                        Integer[] arr = new Integer[2];
                        arr[0] = 1;
                        arr[1] = curCount + tempCount;
                        everyServerCurPageCount.put(serverName, arr);
                        break;
                    } else {
                        Integer[] arr = new Integer[2];
                        if (decCount > tempCount) {
                            decCount -= tempCount;
                            arr[0] = curCount - decCount;
                        } else {
                            arr[0] = 0;
                        }
                        arr[1] = curCount;
                        everyServerCurPageCount.put(serverName, arr);
                        if (tempCount == 0) {
                            break;
                        }
                    }
                }
            }
        }
        return everyServerCurPageCount;
    }



    private DistributePageTotal distributePageTotal;

    public DistributePage() {

    }

    public DistributePage(DistributePageTotal distributePageTotal) {
        this.distributePageTotal = distributePageTotal;
    }

    public DistributePageTotal getDistributePageTotal() {
        return distributePageTotal;
    }

    public void setDistributePageTotal(DistributePageTotal distributePageTotal) {
        this.distributePageTotal = distributePageTotal;
    }
}
